package com.mondo.financialwallet.presentation.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.common.ui.BaseActivity;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.data.entity.MyTokenListEntity;
import com.mondo.financialwallet.presentation.model.Token;
import com.mondo.financialwallet.presentation.event.RequestWalletAssetEvent;
import com.mondo.financialwallet.presentation.ui.adapter.SimpleFragmentPagerAdapter;
import com.mondo.financialwallet.presentation.ui.fragment.TokenTransactionRecordFragment;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;

import org.greenrobot.eventbus.Subscribe;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

import butterknife.Bind;

public class TokenDetailsActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {
    private static final String EXTRA_TOKEN = "TOKEN";

    public static void launch(Context context, Token token) {
        context.startActivity(new Intent(context, TokenDetailsActivity.class)
                .putExtra(EXTRA_TOKEN, token));
    }

    @Bind(R.id.simple_view_pager) ViewPager mSimpleViewPager;
    @Bind(R.id.token_logo_image) ImageView mTokenLogoImage;
    @Bind(R.id.token_name_text_view) TextView mTokenNameTextView;
    @Bind(R.id.token_amount_text_view) TextView mTokenAmountTextView;
    @Bind(R.id.token_convert_text_view) TextView mTokenConvertTextView;
    @Bind(R.id.transfer_layout) LinearLayout mTransferLayout;
    @Bind(R.id.receipt_layout) LinearLayout mReceiptLayout;

    private Token mToken;

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_token_details;
    }

    @Override
    protected void initActions() {
        ActivityTitleBarHelper.init(this, R.string.title_token_details);

        // token 信息
        mToken = (Token) getIntent().getSerializableExtra(EXTRA_TOKEN);
        mTokenNameTextView.setText(mToken.getName());
        mTokenAmountTextView.setText(mToken.getCny());
        mTokenConvertTextView.setText(mToken.getUsd());
        Glide.with(this).load(mToken.getCover()).into(mTokenLogoImage);

        // 转入、转出
        mTransferLayout.setOnClickListener(v ->
                TokenTransferActivity.launch(this, mToken));

        mReceiptLayout.setOnClickListener(v ->
                TokenReceiptActivity.launch(this, mToken.getId()));

        mSimpleViewPager.setAdapter(new SimpleFragmentPagerAdapter(
                getSupportFragmentManager(),
                Arrays.asList("全部", "转入", "转出"),
                Arrays.asList(
                        TokenTransactionRecordFragment.createAllRecord(mToken.getId()),
                        TokenTransactionRecordFragment.createTransferRecord(mToken.getId()),
                        TokenTransactionRecordFragment.createTransferOutRecord(mToken.getId())
                )
        ));
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        startActivity(new Intent(this, TransactionRecordActivity.class));
    }

    @Subscribe
    public void requestWalletAssetEvent(RequestWalletAssetEvent event){
        DataRepository.get().myTokenList(result -> {
            if (result.isSuccess()) {
                List<MyTokenListEntity.DataBean> data = result.getData();
                for (MyTokenListEntity.DataBean datum : data) {
                    if (datum.getToken_id() == Integer.parseInt(mToken.getId())) {
                        mTokenNameTextView.setText(datum.getTitle_en());
                        mTokenAmountTextView.setText(String.valueOf(datum.getBalance()));
                        mTokenConvertTextView.setText("≈$ " + new DecimalFormat("########.#######")
                                        .format(datum.getUsd())
                        );
                        break;
                    }
                }
            }
        });
    }
}
